package com.huaxin.hxmoduleworkorder.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaxin.hxmoduleworkorder.dto.ResponseCode;
import com.huaxin.hxmoduleworkorder.dto.ResponseResult;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionTemplateItemCreateReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionTemplateItemPageListReqDTO;
import com.huaxin.hxmoduleworkorder.dto.request.InspectionTemplateItemUpdateReqDTO;
import com.huaxin.hxmoduleworkorder.mapper.InspectionTemplateItemMapper;
import com.huaxin.hxmoduleworkorder.mapping.InspectionTemplateItemMapping;
import com.huaxin.hxmoduleworkorder.models.InspectionTemplateItem;
import com.huaxin.hxmoduleworkorder.service.InspectionTemplateItemService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class InspectionTemplateItemServiceImpl extends ServiceImpl<InspectionTemplateItemMapper, InspectionTemplateItem> implements InspectionTemplateItemService {
    private final ApplicationContext applicationContext;
    private final InspectionTemplateItemMapping mapping;

    @Override
    public ResponseResult<InspectionTemplateItem> create(InspectionTemplateItemCreateReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数不能为空");
        }
        if (StringUtils.isBlank(in.getCode())) {
            return ResponseResult.parameterError(null, "编码不能为空");
        }
        boolean existed = this.exists(new LambdaQueryWrapper<InspectionTemplateItem>().eq(InspectionTemplateItem::getCode, in.getCode()));
        if (existed) {
            return ResponseResult.failure(ResponseCode.BAD_REQUEST, "编码已存在", null);
        }
        if (StringUtils.isBlank(in.getName())) {
            return ResponseResult.parameterError(null, "名称不能为空");
        }
        InspectionTemplateItem entity = mapping.toEntity(in);
        if (this.save(entity)) {
            InspectionTemplateItem created = this.getById(entity.getId());
            return ResponseResult.ok(created);
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "添加失败，请稍后再试！", null);
        }
    }

    @Override
    public ResponseResult<InspectionTemplateItem> get(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "ID不能为空");
        }
        InspectionTemplateItem existed = this.getById(id);
        if (existed == null) {
            return ResponseResult.notFound(null, "模版项不存在");
        }
        return ResponseResult.ok(existed);
    }

    @Override
    public ResponseResult<String> update(InspectionTemplateItemUpdateReqDTO in) {
        if (in == null) {
            return ResponseResult.parameterError(null, "参数不能为空");
        }
        if (StringUtils.isBlank(in.getId())) {
            return ResponseResult.parameterError(null, "ID不能为空");
        }
        if (StringUtils.isBlank(in.getCode())) {
            return ResponseResult.parameterError(null, "编码不能为空");
        }
        boolean existed = this.exists(new LambdaQueryWrapper<InspectionTemplateItem>()
                .eq(InspectionTemplateItem::getCode, in.getCode())
                .ne(InspectionTemplateItem::getId, in.getId()));
        if (existed) {
            return ResponseResult.failure(ResponseCode.BAD_REQUEST, "编码已存在", null);
        }
        if (StringUtils.isBlank(in.getName())) {
            return ResponseResult.parameterError(null, "名称不能为空");
        }
        if (StringUtils.isBlank(in.getType())) {
            return ResponseResult.parameterError(null, "类型不能为空");
        }
        InspectionTemplateItem entity = this.getById(in.getId());
        mapping.updateEntity(in, entity);
        if (this.updateById(entity)) {
            return ResponseResult.ok("更新成功！");
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "更新失败，请稍后再试！", null);
        }
    }

    @Override
    public ResponseResult<String> delete(String id) {
        if (StringUtils.isBlank(id)) {
            return ResponseResult.parameterError(null, "ID不能为空");
        }
        InspectionTemplateItem entity = this.getById(id);
        if (entity == null) {
            return ResponseResult.notFound(null, "模版项不存在");
        }
        boolean removed = applicationContext.getBean(InspectionTemplateItemService.class).removeById(id);
        if (removed) {
            return ResponseResult.ok("删除成功！");
        } else {
            return ResponseResult.failure(ResponseCode.INTERNAL_SERVER_ERROR, "删除失败，请稍后重试", null);
        }
    }

    @Override
    public ResponseResult<Page<InspectionTemplateItem>> pageList(InspectionTemplateItemPageListReqDTO in) {
        Page<InspectionTemplateItem> page = new Page<>(in.getPageNum(), in.getPageSize());
        LambdaQueryWrapper<InspectionTemplateItem> wrapper = new LambdaQueryWrapper<>();
        // 关键词搜索：同时匹配 编码 或 名称
        if (StringUtils.isNotBlank(in.getKeyword())) {
            wrapper.and(w -> w.like(InspectionTemplateItem::getCode, in.getKeyword())
                    .or()
                    .like(InspectionTemplateItem::getName, in.getKeyword()));
        }
        return ResponseResult.ok(this.page(page, wrapper));
    }
}
